package com.china08.yxyapi.api.manage.stu.mystudy.tifenbao.scoreimprove;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.huixue.YxyHuixuePlan;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.huixue.YxyHuixuePlanRepository;

import io.swagger.annotations.ApiOperation;

/**
 * 首页--学习计划(学习计划列表)-右侧
 * 
 */
@CrossOrigin
@RestController
public class TifenbaoPlanSrhController extends YxyApiBaseController {
    @Autowired
    private YxyHuixuePlanRepository yxyHuixuePlanRepository;

    @ApiOperation(value = "首页 学习计划列表")
    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/stu/mystudy/tifenbao/scoreimprove/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) RepModel reqModel) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        YxyHuixuePlan yxyHuixuePlan = yxyHuixuePlanRepository.findById(reqModel.planId);
        RespModel model = new RespModel();
        model.id = yxyHuixuePlan.getId();
        model.subjectName = yxyHuixuePlan.getSubjectName();
        model.versionName = yxyHuixuePlan.getVersionName();
        model.cebieName = yxyHuixuePlan.getSemesterName();
        model.bookContentName = yxyHuixuePlan.getBookContentName();
        // 开始时间
        Integer beginDate = yxyHuixuePlan.getBeginDate();
        String beginDateString = null;
        if (beginDate != null && beginDate != 0) {
            beginDateString = String.valueOf(beginDate);
            beginDateString = beginDateString.substring(0, 4) + "-" + beginDateString.substring(4, 6) + "-"
                    + beginDateString.substring(6);
        }
        // 结束时间
        Integer endDate = yxyHuixuePlan.getEndDate();
        String endDateString = null;
        if (endDate != null && endDate != 0) {
            endDateString = String.valueOf(endDate);
            endDateString = endDateString.substring(0, 4) + "-" + endDateString.substring(4, 6) + "-"
                    + endDateString.substring(6);
        }
        Date endDate2 = sdf.parse(endDateString);
        model.beginDate = beginDateString;
        model.endDate = endDateString;
        model.status = yxyHuixuePlan.getStatus();
        model.progress = yxyHuixuePlan.getComplete();
        model.usetime = this.getIntervalDays(sdf.parse(beginDateString), endDate2) + 1;// 学习计划用时
        model.scoreType = yxyHuixuePlan.getLastScoreType();
        return model;

    }

    protected static class RepModel extends YxyApiPageReqModel {

        public String planId;// 计划id

    }

    protected static class RespModel extends YxyApiRespModel {
        public String id;// 计划id

        public String subjectName;// 学科名称

        public String versionName;// 版本名称

        public String cebieName;// 册别名称

        public String bookContentName;// 章

        public String beginDate;// 开始时间

        public String endDate;// 结束时间

        public Integer usetime;// 学习计划用时

        public Integer status;// 学习计划的状态：0--已截止 , 1--进行中

        public Double progress;// 计划完成度

        public Integer scoreType;// 分数等级 100,120,150,180

    }

    private Integer getIntervalDays(Date fDate, Date oDate) {

        if (null == fDate || null == oDate) {

        return -1;

        }

        long intervalMilli = oDate.getTime() - fDate.getTime();

        return (int) (intervalMilli / (24 * 60 * 60 * 1000));

    }

}
